package com.gopay.scheduler.job;

import com.gopay.common.constants.dic.GopayGlobalVariablesConstant;
import com.gopay.common.constants.trans.PgStatus;
import com.gopay.common.constants.trans.Trans00700AssistStatus;
import com.gopay.common.dss.tools.FuseTool;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.scheduler.GopayBaseJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.*;

/**
 * 00700 异步做账调用告警
 *
 * @author lx
 * @Tran00700AsyncWarnJob
 * @2017-8-16 13:53:13  www.gopay.com.cn Inc.All rights reserved.
 */

public class Tran00700AsyncWarnJob extends GopayBaseJob {
    private Logger logger = LoggerFactory.getLogger(Tran00700AsyncWarnJob.class);

    @Resource(name = "fuseTool")
    private FuseTool fuseTool;


    @Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;

    private static final int maxFuseTimes = 1;

    private int threshold; //报警阈值

    private long timeSpan; //时间间隔  单位：毫秒

    @Value("#{webProperties['jobCron.recvMobile']}")
    private String recvMobile ;
    @Value("#{webProperties['tran00700Async.WarnEnv']}")
    private String warnEnv;//发送短信的告警环境

    @Override
    protected void doJobInternal() {
        try {
            logger.info("00700 异步做账调用告警查询任务开始...");

            int count = executeQuery();

            logger.info("timeSpan = " + timeSpan + ", threshold = " + threshold + ", queryCount = " + count);

            if (count > threshold) {
                logger.error("触发阈值执行报警...");
                fuseTool.fuse(GopayGlobalVariablesConstant.ASSIST_00700_CHECK_WARN_SWITCH, GopayGlobalVariablesConstant.ASSIST_00700_CHECK_WARN_MOBILE, maxFuseTimes);
            }

            logger.info("00700 异步做账调用告警查询任务结束");

        }catch (Exception e){
            logger.error("00700 异步做账调用告警查询任务异常", e);
        }
    }

    private int executeQuery() {
        Date date = new Date(System.currentTimeMillis() - timeSpan);
        Date dateStart = new Date(date.getTime() - (5 * 60 * 1000) );   // start--[5 * 60 * 1000]--end--[timeSpan]--current

        StringBuffer sb = new StringBuffer();
        sb.append("   select count(*)  ");
        sb.append("     from CPS_TRANS_00700_ASSIST a  ");
        sb.append("     left join cps_gen_main_order o  ");
        sb.append("       on o.gopay_order_id = a.gopay_order_id  ");
        sb.append("     left join PG_GEN_ORDER_STATUS_DTL os  ");
        sb.append("       on os.id = o.trans_dtl_sq  ");
        sb.append("    where a.status = '" + Trans00700AssistStatus.INIT.value + "'  ");
        sb.append("      and os.status = '" + PgStatus.SUCCESS.value + "'  ");
        sb.append("      and o.txn_sta_cd = 30000 ");  //订单状态为30000
        sb.append("      and o.accounting_point is null  ");  //记账点为空
        sb.append(" and a.created_ts <= :date");
        sb.append(" and a.created_ts >= :dateStart");

        Map<String, Object> param = new HashMap<>();
        param.put("date", date);
        param.put("dateStart", dateStart);

        int count = commonQueryDAO.findCountBySql(sb.toString(), param);

        return count;

    }

    public int getThreshold() {
        return threshold;
    }

    public void setThreshold(int threshold) {
        this.threshold = threshold;
    }

    public long getTimeSpan() {
        return timeSpan;
    }

    public void setTimeSpan(long timeSpan) {
        this.timeSpan = timeSpan;
    }
}
